﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Configuration;

namespace Inbox.Domain
{
    public class InboxContext : DataContext
    {
        public static readonly string strBaseConnection = ConfigurationManager.ConnectionStrings["InboxConnectionString"].ConnectionString;
        public string ConnectionString
        {
            get
            {
                return strBaseConnection;
            }
        }

        public InboxContext()
            : this(strBaseConnection)
        {
        }

        public InboxContext(string strConnection)
            : base(strConnection)
        {

        }

        public void InsertEntity<T>(T entity) where T : class
        {
            try
            {
                Table<T> table = this.GetTable<T>();
                table.InsertOnSubmit(entity);
                this.SubmitChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public bool DeleteEntity<T>(T entity) where T : class
        {
            try
            {
                Table<T> table = this.GetTable<T>();
                
                table.DeleteOnSubmit(entity);
                this.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public bool UpdateEntity(PersistentObject entity)
        {
            try
            {
                Table<PersistentObject> table = this.GetTable<PersistentObject>();
                table.Attach(entity, true);
                this.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}
